home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_052 / mxgads / mxgads.c < prev   
C/C++ Source or Header  |  1992-05-06  |  7KB  |  378 lines

  1. /** testmxgads.c
  2. *
  3. *   This program gives a demonstration of how to make mutual exclusion
  4. *   of boolean gadgets work even if they contain GadgetText.
  5. *   The method is still somewhat of a cludge, but then, it will last
  6. *   until 1.3 comes out... Hope this proves of some use.
  7. *                                       Willy.
  8. *
  9. *   The usual disclaimer:
  10. *
  11. *               Copyright 1986,  W.G.J. Langeveld
  12. *
  13. *   Permission is granted for unlimited commercial and/or noncommercial
  14. *   use. This program  may  not  be uploaded  to  any   commercial  BBS
  15. *   except Byte Information Exchange. The author accepts no responsibi-
  16. *   lity for this program whatsoever.
  17. *
  18. **/
  19.  
  20.  
  21. #include "exec/types.h"
  22. #include "exec/io.h"
  23. #include "exec/memory.h"
  24. #include "libraries/dos.h"
  25. #include "intuition/intuition.h"
  26. #include "functions.h"
  27.  
  28. /* The following source fragment was generated by PowerWindows */
  29.  
  30. SHORT BorderVectors1[] = {0,0,48,0,48,14,0,14,0,0};
  31. struct Border Border1 = {
  32.    -2,-1,
  33.    3,0,JAM1,
  34.    5,
  35.    BorderVectors1,
  36.    NULL
  37. };
  38.  
  39. struct IntuiText IText1 = {
  40.    1,0,JAM2,
  41.    6,3,
  42.    NULL,
  43.    (UBYTE *)"Quit",
  44.    NULL
  45. };
  46.  
  47. struct Gadget Gadg5 = {
  48.    NULL,
  49.    89,51,
  50.    45,13,
  51.    GADGHCOMP,
  52.    RELVERIFY,
  53.    BOOLGADGET,
  54.    (APTR)&Border1,
  55.    NULL,
  56.    &IText1,
  57.    0,
  58.    NULL,
  59.    7,
  60.    NULL
  61. };
  62.  
  63. SHORT BorderVectors2[] = {0,0,48,0,48,14,0,14,0,0};
  64. struct Border Border2 = {
  65.    -2,-1,
  66.    3,0,JAM1,
  67.    5,
  68.    BorderVectors2,
  69.    NULL
  70. };
  71.  
  72. struct IntuiText IText2 = {
  73.    3,0,INVERSVID,
  74.    7,3,
  75.    NULL,
  76.    (UBYTE *)"Four",
  77.    NULL
  78. };
  79.  
  80. struct Gadget Gadg4 = {
  81.    &Gadg5,
  82.    166,18,
  83.    45,13,
  84.    GADGHCOMP,
  85.    GADGIMMEDIATE,
  86.    BOOLGADGET,
  87.    (APTR)&Border2,
  88.    NULL,
  89.    &IText2,
  90.    0,
  91.    NULL,
  92.    6,
  93.    NULL
  94. };
  95.  
  96. SHORT BorderVectors3[] = {0,0,48,0,48,14,0,14,0,0};
  97. struct Border Border3 = {
  98.    -2,-1,
  99.    3,0,JAM1,
  100.    5,
  101.    BorderVectors3,
  102.    NULL
  103. };
  104.  
  105. struct IntuiText IText3 = {
  106.    2,0,INVERSVID,
  107.    2,3,
  108.    NULL,
  109.    (UBYTE *)"Three",
  110.    NULL
  111. };
  112.  
  113. struct Gadget Gadg3 = {
  114.    &Gadg4,
  115.    114,18,
  116.    45,13,
  117.    GADGHCOMP,
  118.    GADGIMMEDIATE,
  119.    BOOLGADGET,
  120.    (APTR)&Border3,
  121.    NULL,
  122.    &IText3,
  123.    0,
  124.    NULL,
  125.    5,
  126.    NULL
  127. };
  128.  
  129. SHORT BorderVectors4[] = {0,0,48,0,48,14,0,14,0,0};
  130. struct Border Border4 = {
  131.    -2,-1,
  132.    3,0,JAM1,
  133.    5,
  134.    BorderVectors4,
  135.    NULL
  136. };
  137.  
  138. struct IntuiText IText4 = {
  139.    1,0,JAM1,
  140.    9,3,
  141.    NULL,
  142.    (UBYTE *)"One",
  143.    NULL
  144. };
  145.  
  146. struct Gadget Gadg1 = {
  147.    &Gadg3,
  148.    10,18,
  149.    45,13,
  150.    GADGHCOMP+SELECTED,
  151.    GADGIMMEDIATE,
  152.    BOOLGADGET,
  153.    (APTR)&Border4,
  154.    NULL,
  155.    &IText4,
  156.    0,
  157.    NULL,
  158.    4,
  159.    NULL
  160. };
  161.  
  162. SHORT BorderVectors5[] = {0,0,48,0,48,14,0,14,0,0};
  163. struct Border Border5 = {
  164.    -2,-1,
  165.    3,0,JAM1,
  166.    5,
  167.    BorderVectors5,
  168.    NULL
  169. };
  170.  
  171. struct IntuiText IText5 = {
  172.    3,0,JAM2,
  173.    8,3,
  174.    NULL,
  175.    (UBYTE *)"Two",
  176.    NULL
  177. };
  178.  
  179. struct Gadget Gadg2 = {
  180.    &Gadg1,
  181.    62,18,
  182.    45,13,
  183.    GADGHCOMP,
  184.    GADGIMMEDIATE,
  185.    BOOLGADGET,
  186.    (APTR)&Border5,
  187.    NULL,
  188.    &IText5,
  189.    0,
  190.    NULL,
  191.    3,
  192.    NULL
  193. };
  194.  
  195. struct NewWindow NewWindowStructure = {
  196.    351,39,
  197.    220,70,
  198.    0,1,
  199.    GADGETDOWN+GADGETUP+CLOSEWINDOW,
  200.    WINDOWDRAG+WINDOWCLOSE+ACTIVATE,
  201.    &Gadg2,
  202.    NULL,
  203.    (UBYTE *)"Mutual Exclude Test",
  204.    NULL,
  205.    NULL,
  206.    5,5,
  207.    640,200,
  208.    WBENCHSCREEN
  209. };
  210.  
  211. /* end of PowerWindows source generation */
  212.  
  213. #define GAD1 4
  214. #define GAD2 3
  215. #define GAD3 5
  216. #define GAD4 6
  217. #define GAD5 7
  218.  
  219. struct IntuitionBase *IntuitionBase = 0L;
  220.  
  221. struct Window *twindow;
  222. void GadMXSel(), GadMXSD();
  223.  
  224. main()
  225. {
  226.    IntuitionBase = (struct IntuitionBase *) 
  227.                     OpenLibrary("intuition.library", 0L);
  228.    if (IntuitionBase == NULL) {
  229.       printf("Failed to open intuition\n");
  230.       exit();
  231.    }
  232.  
  233.    Test();  /* Do the work: put up a "requester" */
  234.  
  235.    if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  236.    return(0);
  237. }
  238.  
  239. /**
  240. *
  241. *   Routine to put up the "Requester" and handle IDCMP messages.
  242. *
  243. **/
  244. Test()
  245. {
  246.    static ULONG class;
  247.  
  248.    static struct IntuiMessage *message;
  249.    static struct Gadget *gad;
  250.    static int looping;
  251.  
  252.    twindow = OpenWindow(&NewWindowStructure); /* open the "Requester" */
  253.    if ( twindow == NULL ) {
  254.       printf ("open window failed\n");
  255.       return;
  256.    }
  257.  
  258.    looping = TRUE;
  259.    while (looping) {
  260.       WaitPort(twindow->UserPort);
  261.       while((message = (struct IntuiMessage *)
  262.                        GetMsg(twindow->UserPort)) != NULL) {
  263.          class = message->Class;
  264.          gad = (struct Gadget *)message->IAddress;
  265.          ReplyMsg(message);
  266.  
  267.          if (class == CLOSEWINDOW) looping = FALSE;
  268.  
  269.          if ((class == GADGETUP) || (class == GADGETDOWN)) {
  270.             switch (gad->GadgetID) {
  271.                case GAD5 :
  272.                   printf("QUIT\n");
  273.                   looping = FALSE;
  274.                   break;
  275.                case GAD1 :
  276.                   printf("ONE\n");
  277.                   GadMXSel(twindow,&Gadg1,&Gadg2,&Gadg3,&Gadg4,NULL,NULL);
  278.                   break;
  279.                case GAD2 :
  280.                   printf("TWO\n");
  281.                   GadMXSel(twindow,&Gadg2,&Gadg1,&Gadg3,&Gadg4,NULL,NULL);
  282.                   break;
  283.                case GAD3 :
  284.                   printf("THREE\n");
  285.                   GadMXSel(twindow,&Gadg3,&Gadg1,&Gadg2,&Gadg4,NULL,NULL);
  286.                   break;
  287.                case GAD4 :
  288.                   printf("FOUR\n");
  289.                   GadMXSel(twindow,&Gadg4,&Gadg1,&Gadg2,&Gadg3,NULL,NULL);
  290.                   break;
  291.             }
  292.          }
  293.       }
  294.    }
  295.    CloseWindow(twindow);
  296.    return;
  297. }
  298.  
  299. /**
  300. *
  301. *   This routine selects gad1 and deselects the previously selected one.
  302. *   Maximum number of gadgets to be mutually excluded is six, obviously.
  303. *   Extension to more gadgets is obvious, too. This routine assumes that
  304. *   only one gadget is selected at a time.
  305. *
  306. **/
  307. void GadMXSel(win,gad1,gad2,gad3,gad4,gad5,gad6)
  308. struct Window *win;
  309. struct Gadget *gad1,*gad2,*gad3,*gad4,*gad5,*gad6;
  310. {
  311.    static struct Gadget *gadprev;
  312.  
  313.    gadprev = NULL;
  314.    if (gad1 != NULL)
  315.       if (gad1->Flags & SELECTED) return;
  316.    if (gad2 != NULL)
  317.       if (gad2->Flags & SELECTED) gadprev = gad2; 
  318.    if (gad3 != NULL)
  319.       if (gad3->Flags & SELECTED) gadprev = gad3;
  320.    if (gad4 != NULL)
  321.       if (gad4->Flags & SELECTED) gadprev = gad4;
  322.    if (gad5 != NULL)
  323.       if (gad5->Flags & SELECTED) gadprev = gad5;
  324.    if (gad6 != NULL) 
  325.       if (gad6->Flags & SELECTED) gadprev = gad6;
  326.  
  327.    GadMXSD(win,gad1,gadprev);
  328.  
  329.    return;
  330. }
  331.  
  332. /**
  333. *
  334. *   This routine selects gad1 and deselects gad2.
  335. *   Notice, that this version removes gadgets from the gadget list and adds
  336. *   them to the end. If you're sensitive to the location of the gadgets
  337. *   in the gadgetlist, you have to use: gadloc = RemoveGadget... and
  338. *   AddGadget(...,...,(long) gadloc), where gadloc is a USHORT. Then you
  339. *   have to refresh all gadgets to make sure the two you changed get
  340. *   refreshed.
  341. *
  342. **/
  343. void GadMXSD(win,gad1,gad2)
  344. struct Window *win;
  345. struct Gadget *gad1,*gad2;
  346. {
  347. /*
  348. *   First select gad2 (yes!) and refresh.
  349. */
  350.    if (gad2 != NULL) {
  351.       RemoveGadget(win,gad2);
  352.       gad2->Flags |= SELECTED;
  353.       AddGadget(win,gad2,-1L);
  354.    }
  355.  
  356.    if (gad2 != NULL) RefreshGadgets(gad2,win,NULL);
  357. /*
  358. *   Now select gad1 and deselect gad2 and refresh.
  359. */
  360.    if (gad1 != NULL) {
  361.       RemoveGadget(win,gad1);
  362.       gad1->Flags |= SELECTED;
  363.       AddGadget(win,gad1,-1L);
  364.    }
  365.  
  366.    if (gad2 != NULL) {
  367.       RemoveGadget(win,gad2);
  368.       gad2->Flags &= ~SELECTED;
  369.       AddGadget(win,gad2,-1L);
  370.    }
  371.  
  372.    if (gad1 != NULL) RefreshGadgets(gad1,win,NULL);
  373.  
  374.    return;
  375. }
  376.  
  377.